<template>
	<div class='main'>
		<div style="width: 100% !important;">
			<!-- 行内产品需求 productCode-->
			<ActiveForm required ref='productCode' >
				<view class="active_label">
					行内产品：
				</view>
				<view class="active_input">
					<SelectPicker @change='pChange'  v-model="colonyInfo.productCode" :options="productArr" placeholder="请选择" />
				</view>
			</ActiveForm>
			
		</div>
		<ActiveForm :required='zProject' ref='colonyCode'>
			<view class="active_label">
				集群：
			</view>
			<view class="active_input">
				<SelectPicker disabled v-model="colonyInfo.colonyCode" :options="colonyarr" placeholder="请先选择行内产品" />
			</view>
		</ActiveForm>
		<ActiveForm :required='zProject' ref='partnerCode'>
			<view class="active_label">
				合作方：
			</view>
			<view class="active_input">
				<SelectPicker :disabled="!zProject"  @change='partnerChange($event)'  v-model="colonyInfo.partnerCode" :options="arr" placeholder="请选择" />
			</view>
		</ActiveForm>
		<ActiveForm required ref='isGuarantee' style="width: 100% !important;">
			<radio-group @change="changeFn">
				<text style="font-size: 3rem;">合作方是否提供担保：</text>
				<label class="radio" style="font-size: 3rem;">
					<radio v-if="isResh" :disabled='partnerDisabled' value="Y" :checked="colonyInfo.isGuarantee=='Y'" />是
				</label>
				<label class="radio" style="margin-left: 2rem;font-size: 3rem;">
					<radio v-if="isResh" :disabled='partnerDisabled' value="N" :checked="colonyInfo.isGuarantee=='N'" />否
				</label>
			</radio-group>
			
		</ActiveForm>
		
		<block v-if="sceneType==5">
			<view class="active_tit">
				<view class="tit">
					<text style="color: red;">*</text>情况说明：
				</view> 
			</view>
			<!-- input -->
			<ActiveForm required  ref='marks' style="width: 100% !important;">	
				<view class="active_input">
					<textarea maxlength="1000" placeholder='最多输入1000字' v-model="colonyInfo.marks" auto-height style="min-height: 20rem;width: 100%;font-size: 2.6rem;" />
				</view>
			</ActiveForm>
			
		</block>
		
		
		
	</div>
	
</template>

<script>
	import SelectPicker from "../../SelectPicker";
	export default{
		data(){
			return{
				loanType:'4',
				zProject:true,
				productArr:[],
				// =========
				isResh:true,
				isdisabled:false,
				colonyInfo:{
					isGuarantee:'',
					colonyCode:'',
					partnerCode:'',
					marks:'',
				},
				colonyarr:[],
				arr:[],
				partnerDisabled:false,
				isFirst:true,
				
				
			}
			
		},
		props:{
			sceneType:''
		},
		components: {
			SelectPicker,
		},
		watch: {
			sceneType:{
				handler: function(res) {
					console.warn('sceneType', res)
					this.isResh = false
					setTimeout(()=>{
						this.isResh = true	
					},300)
					this.partnerDisabled = false
					this.getProductArr()
				},
				deep: true
			},
			colonyInfo: {
				handler: function(res) {
					console.warn('td', res)
					getApp().globalData.stdOffsurColonyInfo = res
				},
				deep: true
			},
			
		},
		mounted() {
			setTimeout(()=>{
				this.loanType = getApp().globalData.stdOffsurBaiscInfo.loanType
				this.colonyInfo = getApp().globalData.stdOffsurColonyInfo

		        //根据集群获取合作方
				if(this.colonyInfo.colonyCode){
					this.getpartnerArr(this.colonyInfo.colonyCode,'first')	
					
				}
				
				if(this.sceneType=='1'||this.sceneType=='2'||this.sceneType=='3'){
					this.isdisabled = true
				}else{
					this.isdisabled = false
				}
				
				
			},getApp().globalData.timeout)
			
			
			
		},
		methods:{
			pChange(e){
				console.log(e,'产品监听')
				let proName = ''
				// 根据value 获取数据，集群 编号，名称  是否专案
				for(let i=0;i<this.productArr.length;i++){
					if(this.productArr[i].value===e){
						proName = this.productArr[i].label
						this.colonyInfo.productName= this.productArr[i].label
						// 默认赋值集群显示
						this.colonyarr = [
							{
								label:this.productArr[i].colonyName,
							    value:this.productArr[i].colonyNo
							}
						]
						this.colonyInfo.colonyCode = this.productArr[i].colonyNo
						
						//根据集群查询合作方
						this.getpartnerArr(this.colonyInfo.colonyCode)
						
						//判断是否是专案制
						if(this.productArr[i].isProject==='0'){
							//是专案
							this.zProject = true
							getApp().globalData.isProject = true
							//若行内产品为“专案制”产品，但此时无法映射出“集群”信息，则提示“系统未找到集群信息，请前往行内产品管理页为该产品配置集群信息！”
							if(this.productArr[i].colonyNo===''||this.productArr[i].colonyNo===null){
								uni.showToast({
									title: '系统未找到集群信息，请联系业务发展部相关人员前往行内产品管理界面为该产品配置集群信息！',
									icon: "none",
									duration: 2500
								})
							}
							
						}else{
							this.zProject = false
							getApp().globalData.isProject = false
							this.colonyInfo.colonyCode = ''
							this.colonyInfo.partnerCode = ''
						}
						
						break
					}
					
				}
				
				if(this.loanType!=='4'){
					// 产品校验 offsur/checkProStsByCode
					this.sendRequest({
						url: 'offsur/checkProStsByCode',
						method: 'post',
						hideLoading: true,
						data: {
							productCode:e,
							productName:proName
						},
						complete: (res) => {
							if(res.code==='200'){
								if(res.data.prdStsCode!=='02'){
									this.colonyInfo.productCode = ''
									uni.showToast({
										title: '该产品未生效，请重新选择',
										icon: "none",
										duration: 2500
									})
								} 
							}else{
								this.colonyInfo.productCode = ''
								uni.showToast({
									title: res.message,
									icon: "none",
									duration: 2500
								})
								
							}
					
						}
					})
					
				}
				
				
				
				
				
			},
			//根据选择的集群请求产品
			getProductArr(id){	
				if(!this.isFirst){
					this.colonyarr = []	
					this.colonyInfo.productCode = ''
					this.colonyInfo.colonyCode = ''
					this.colonyInfo.partnerCode = ''
					this.colonyInfo.isGuarantee = ''
				}
				//测试数据
				// this.isFirst = false
				// this.productArr = [
				// 	{label:'测试1',value:'1',colonyName:'集群',colonyNo:'1',isProject:'0'}
				// ]

				let param = {}
				if(this.sceneType!='4'){
					param =  {
						reqId:getApp().globalData.reqId,
						scene:this.sceneType,
						isProject:'0',
					}
				}else{
					param = {
						reqId:getApp().globalData.reqId,
						scene:this.sceneType,
					}
				}
				this.sendRequest({
					url: 'productRuleConfig/getProducts',
					method: 'post',
					hideLoading: true,
					data:param,
					complete: (res) => {
						if(res.code==='200'){
							for(let i=0;i<res.data.length;i++){
								res.data[i].label = res.data[i].productName
								res.data[i].value = res.data[i].productCode
							}
							
							this.productArr = res.data
							
							//根据产品id确定数据,调查表进入判断是否反显数据。
							
							if(this.isFirst){
								if(this.colonyInfo.productCode){
									this.pChange(this.colonyInfo.productCode)
								}	
							}
							
							
							this.isFirst = false
						}
				
					}
				})
				
			},
			changeFn(e){
				console.log(e)
				this.colonyInfo.isGuarantee = e.detail.value
			},
			colonyChange(e){
				console.log(e)
				this.getpartnerArr(e)
			},
			partnerChange(e){
				console.log(e)
				this.isResh = false
				setTimeout(()=>{
					this.isResh = true	
				},300)
				
				// this.colonyInfo.partnerCode = e
				this.colonyInfo.isGuarantee = null
			
				for(let i=0;i<this.arr.length;i++){
					if(e===this.arr[i].partnerId){
					 
					 if(this.arr[i].isGuarantee==='Y'||this.arr[i].isGuarantee==='N'){
						 this.colonyInfo.isGuarantee = this.arr[i].isGuarantee
						 this.partnerDisabled = true
					 }else{
						 
						 if(this.arr[i].isGuarantee==='S'){
							 this.colonyInfo.isGuarantee = null
						 }else{
							 this.colonyInfo.isGuarantee = this.arr[i].isGuarantee
						 }
						 this.partnerDisabled = false
					 }
					 
					 
					 //商用车场景下 同步挂靠物流公司
					 if(this.sceneType==1){
					 	let pCont = {
					 		label:this.arr[i].partnerName,
							value:e
					 	}
					 	this.$emit('getPartnerId',pCont)
					 }
					 
					 
					 
					}
					
					
				}
				
				
			},
			// 通用场景需求开发
			getColony(id){
				this.arr = []
				this.sendRequest({
					url: 'offsur/colony/getColonyByScene',
					method: 'post',
					hideLoading: true,
					data: {
						sceneType:id
					},
					complete: (res) => {
						if(res.code==='200'){
							this.colonyarr = res.data
							
							for(let i=0;i<res.data.length;i++){
								res.data[i].label = res.data[i].colonyName
								res.data[i].value = res.data[i].colonyNo
							}
							
							
							
							//判断场景
							if(this.sceneType==='1'||this.sceneType==='2'||this.sceneType==='3'){
								
							
								if(this.colonyarr.length>0){
									this.colonyInfo.colonyCode  = this.colonyarr[0].value
									
									this.getpartnerArr(this.colonyInfo.colonyCode)
									//新增需求 行内产品需求
									this.getProductArr(this.colonyInfo.colonyCode)
								}
								
								
								this.isdisabled = true
								
								if(!this.isFirst){
									this.colonyInfo.isGuarantee=''
									this.colonyInfo.partnerCode=''
									this.colonyInfo.marks=''
									this.colonyInfo.productCode = ''
	
								}
								
							}else if(this.sceneType==='5'){
								this.isdisabled = false
								if(!this.isFirst){
									this.colonyInfo.isGuarantee = ''
									this.colonyInfo.colonyCode = ''
									this.colonyInfo.partnerCode = ''
									this.colonyInfo.marks = ''
									this.colonyInfo.productCode = ''
									this.productArr = []
								}
								
							}else{
								this.isdisabled = false
								if(!this.isFirst){
									this.colonyInfo.isGuarantee = ''
									this.colonyInfo.colonyCode = ''
									this.colonyInfo.partnerCode = ''
									this.colonyInfo.marks = ''
									this.colonyInfo.productCode = ''
									
								}
							}
							
							
							this.isFirst = false //判断是不是初次加载
						}

					}
				})
			},
			getpartnerArr(id,flag){
				//根据选择的集群请求合作方
				this.sendRequest({
					url: 'offsur/colony/getPartnerByColonyCode',
					method: 'post',
					hideLoading: true,
					data: {
						colonyCode:id,
						loginName:getApp().globalData.userId||this.colonyInfo.loginName
					},
					complete: (res) => {
						if(res.code==='200'){
							if(res.data.length===0){
								uni.showToast({
									title: '系统未找到合作方信息，请联系业务发展部相关人员前往行内产品管理界面为该产品配置合作方信息！',
									icon: "none",
									duration: 2500
								})
							}
							for(let i=0;i<res.data.length;i++){
								res.data[i].label = res.data[i].partnerName
								res.data[i].value = res.data[i].partnerId
							}
							
							this.arr = res.data
							//判断是否提供担保 isGuarantee
						}
				
					}
				})
			}
		}
	}
</script>

<style>
</style>
